package org.seedltd.upms.controller.detection;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.seedltd.core.base.BaseController;
import org.seedltd.core.base.BasePage;
import org.seedltd.core.utils.Assert;
import org.seedltd.upms.model.detection.form.DetectionQrcodeForm;
import org.seedltd.upms.model.detection.form.DetectionQrcodeSearchForm;
import org.seedltd.upms.model.detection.vo.DetectionQrcodeVO;
import org.seedltd.upms.service.detection.DetectionQrcodeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.itextpdf.text.DocumentException;

import lombok.AllArgsConstructor;

/**
 *
 * 二维码数据控制器
 * 
 * @author laizuan
 * @version 1.0
 * @since 2021/07/26
 */
@RestController
@AllArgsConstructor
@RequestMapping("/detectionQrcode")
public class DetectionQrcodeController extends BaseController<DetectionQrcodeService> {

    /**
     * 新增
     * 
     * @param form
     *            生产二维码张数
     */
    @PostMapping(value = "/add")
    @PreAuthorize("hasAuthority('detection:qrcode:add')")
    public boolean add(@RequestBody @Validated DetectionQrcodeForm form) throws Exception {
        baseService.add(form.getCodeNumber());
        return true;
    }

    /**
     * 修改数据
     *
     * @param id
     *            主键
     * @return 是否修改成功。 true: 成功，false:失败
     */
    @PostMapping(value = "/update/{id}")
    @PreAuthorize("hasAuthority('detection:qrcode:update')")
    public boolean update(@PathVariable Long id) {
        return baseService.disabled(id);
    }

    /**
     * 批量打印二维码
     *
     * @param response
     *            响应流
     * @param ids
     *            id 字符串主键
     * @param type
     *            二维码类型
     */
    @GetMapping("/print")
    public void print(HttpServletResponse response, String ids, String type) throws IOException, DocumentException {
        baseService.print(response, ids, type);
    }

    /**
     * 批量删除
     * 
     * @param idList
     *            id集合
     * @return
     */
    @PreAuthorize("hasAuthority('detection:qrcode:delete')")
    @PostMapping(value = "/deleteBatch")
    public boolean deleteBatch(@RequestBody Long[] idList) {
        for (Long id : idList) {
            baseService.delete(id);
        }
        return true;
    }

    /**
     * 删除
     * 
     * @param id
     *            二维码数据主键
     * @return 删除状态。true标识修改成功，false标识修改失败
     */
    @GetMapping(value = "/delete")
    @PreAuthorize("hasAuthority('detection:qrcode:delete')")
    public boolean delete(Long id) {
        Assert.notNull(id, "ID不能为空");
        return baseService.delete(id);
    }

    /**
     * 列表
     * 
     * @param searchForm
     *            查询条件
     * @return 二维码数据列表集合
     */
    @PostMapping(value = "/list")
    public BasePage<DetectionQrcodeVO> page(@RequestBody DetectionQrcodeSearchForm searchForm) {
        return baseService.page(searchForm);
    }

}